﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using Zadani09.db.ODS;

namespace Zadani09.umelec
{
    public partial class Serie : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            ObjectDataSource2.Updated += new ObjectDataSourceStatusEventHandler(ObjectDataSource2_Updated);
            ObjectDataSource2.Inserted += new ObjectDataSourceStatusEventHandler(ObjectDataSource2_Inserted);
            Button2.Click += new EventHandler(Button2_Click);
            Button3.Click += new EventHandler(Button3_Click);
        }

        //odstraneni ze serie
        void Button3_Click(object sender, EventArgs e)
        {
            ObjectDataSource4.Delete();
            GridView2.DataBind();
            DropDownList4.DataBind();
        }

        //pridani do serie
        void Button2_Click(object sender, EventArgs e)
        {
            ObjectDataSource5.Insert();
            GridView2.DataBind();
            DropDownList5.DataBind();
        }

        void ObjectDataSource2_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            aktualizujData();
        }

        void ObjectDataSource2_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            aktualizujData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //uplne ze zacatku vypnout pridani / odebrani, ktere se vyhodnoti v zapeti podle
            //vykonane akce v jine casti kodu
            schovejPridaniOdebrani();
        }

        private void aktualizujData()
        {
            ObjectDataSource1.Select();
            GridView1.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            GridView1.DataBind();
            if (GridView1.Rows.Count <= 0)
            {
                DetailsView1.ChangeMode(DetailsViewMode.Insert);
                schovejVyhledavani();
                schovejPridaniOdebrani();
            }
            else if (DetailsView1.CurrentMode != DetailsViewMode.Edit &&
                     GridView1.SelectedIndex <= 0 &&
                     DetailsView1.DataItemIndex >= 0)
            {
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);

                GridView1.SelectedIndex = 0;
                ObjectDataSource2.Select();
                DetailsView1.DataBind();

                DetailsView1.PageIndex = 0;
                zobrazVyhledavani();
                zobrazPridaniOdebrani();
            }
            else
            {
                //v pripade ze byl selektnuta nejaka serie, mimo prvni
                zobrazPridaniOdebrani();
            }

            if (GridView1.Rows.Count > 0) //zapni vyhledavani vzdy kdyz je co hledat..
            {
                zobrazVyhledavani();
            }

            base.OnPreRender(e);
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            Session["vyhl_podle_admin_serie"] = IsPostBack ? DropDownList1.SelectedValue : DropDownList1.Items[0].Value;

            base.OnPreRenderComplete(e);
        }

        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetCompletionList(string prefixText, int count, string contextKey)
        {
            SerieDataObject serie = new SerieDataObject();
            List<Zadani09.db.Serie> vysl;
            List<string> vrat = new List<string>();

            vysl = serie.cti_s_vyhledavanim(null, prefixText,
                        (string)HttpContext.Current.Session["vyhl_podle_admin_serie"]);

            foreach (Zadani09.db.Serie jeden in vysl)
                vrat.Add(jeden[HttpContext.Current.Session["vyhl_podle_admin_serie"].ToString()].ToString());

            return (vrat.ToArray());
        }

        private void schovejVyhledavani()
        {
            UpdatePanel1.Visible = false;
            Button1.Visible = false;
            DropDownList1.Visible = false;

        }

        private void zobrazVyhledavani()
        {
            UpdatePanel1.Visible = true;
            Button1.Visible = true;
            DropDownList1.Visible = true;

        }

        private void schovejPridaniOdebrani()
        {
            Panel1.Visible = false;
            schovejOdebiraniZeSerie();
            schovejPridavaniDoSerie();
        }

        private void zobrazPridaniOdebrani()
        {
            Panel1.Visible = true;

            DropDownList4.DataBind();

            if (DropDownList4.Items.Count <= 0)
            {
                schovejPridavaniDoSerie();
            }
            else
            {
                zobrazPridavaniDoSerie();
            }

            DropDownList5.DataBind();

            if (DropDownList5.Items.Count <= 0)
            {
                schovejOdebiraniZeSerie();
            }
            else
            {
                zobrazOdebiraniZeSerie();
            }
        }
        private void schovejPridavaniDoSerie()
        {
            DropDownList4.Visible = false;
            Button2.Visible = false;
        }

        private void schovejOdebiraniZeSerie()
        {
            DropDownList5.Visible = false;
            Button3.Visible = false;
        }

        private void zobrazPridavaniDoSerie()
        {
            DropDownList4.Visible = true;
            Button2.Visible = true;
        }

        private void zobrazOdebiraniZeSerie()
        {
            DropDownList5.Visible = true;
            Button3.Visible = true;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox1.Text = ViewState["vyhl_admin_serie"] == null ? "" : (string)ViewState["vyhl_admin_serie"];
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ViewState["vyhl_admin_serie"] = TextBox1.Text;
        }
    }
}
